package com.haiyou.dc.thread.test3;

public class TestThreadLocal01 {

//    private static ThreadLocal<Integer> threadLocal = new ThreadLocal(){
//        @Override
//        protected Object initialValue() {
//            return 20;
//        }
//    };

    private static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 200);

    public static void main(String[] args) {

        System.out.println(Thread.currentThread().getName() + "------>" + threadLocal.get());
        threadLocal.set(300);
        System.out.println(Thread.currentThread().getName() + "------>" + threadLocal.get());

        new Thread(new MyRun()).start();
        new Thread(new MyRun()).start();
    }

    static class MyRun implements Runnable {

        @Override
        public void run() {
            threadLocal.set((int) (Math.random() * 99));
            System.out.println(Thread.currentThread().getName() + "------>" + threadLocal.get());
        }
    }

}
